package com.yht.downloadimage.runmain;

import java.io.IOException;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.yht.downloadimage.httpClient.HttpClientManager;

public class Test {

	public static void main(String[] args) throws ClientProtocolException, IOException {
		CloseableHttpClient httpClient = HttpClientManager.getConnection();
		 getRequest(httpClient, "https://bj.lianjia.com/ershoufang/dongcheng/");
	}

	public static void getRequest(CloseableHttpClient httpClient,String url) throws ClientProtocolException, IOException {
		HttpGet get=new HttpGet(url);
		CloseableHttpResponse execute = httpClient.execute(get);
		Document doc = Jsoup.parse(EntityUtils.toString(execute.getEntity()));
		Elements as = doc.select("div[data-role=\"ershoufang\"] div").get(0).select("a");
		for(Element a:as) {
			String href=a.attr("href");
			if(!href.startsWith("http")) {
				href="https://bj.lianjia.com"+href;
			}
			System.out.println("seeds.add(\""+href+"\");");
		}
		
		for(Element a:as) {
			String text = a.text();
			String href=a.attr("href");
			if(!href.startsWith("http")) {
				href="https://bj.lianjia.com"+href;
			}
			String[] split = href.split("/");
			String ear=split[split.length-1];
			System.out.println("areaMap.put(\""+ear+"\",\""+text+"\");");
		}
	}
	
	
	public static HttpResponse requestPost(CloseableHttpClient httpClient,String url,Map<String,String> requestParams) throws ClientProtocolException, IOException{
		String[] split = url.split("\\?");
		String urlT=url.split("\\?")[0];
		String paramsStr="{\"channel\":\"news\",\"url\":\"http://renjian.163.com/17/0823/15/CSHLHHHB000181RV.html\",\"expireTime\":20}";
		HttpPost postMethod=new HttpPost(url);
		postMethod.addHeader("Content-Type","application/json");
		System.out.println("paramsStr="+paramsStr);
		StringEntity entity=new StringEntity(paramsStr, "utf-8");
		postMethod.setEntity(entity);
		return httpClient.execute(postMethod);
	}
	
	
	public static HttpResponse requestPut(CloseableHttpClient httpClient,String url,Map<String,String> requestParams) throws ClientProtocolException, IOException{
		String paramsStr="{\"channel\":\"news\",\"url\":\"http://renjian.163.com/17/0823/15/CSHLHHHB000181RV.html\"}";
		HttpPut putMethod=new HttpPut(url);
		putMethod.addHeader("Content-Type","application/json");
		System.out.println("paramsStr="+paramsStr);
		StringEntity entity=new StringEntity(paramsStr, "utf-8");
		putMethod.setEntity(entity);
		return httpClient.execute(putMethod);
	}
	
//	public static HttpResponse requestDelete(CloseableHttpClient httpClient,String url,Map<String,String> requestParams) throws ClientProtocolException, IOException{
//		String[] split = url.split("\\?");
//		String urlT=url.split("\\?")[0];
//		String paramsStr="{\"channel\":\"news\",\"url\":\"http://renjian.163.com/17/0823/15/CSHLHHHB000181RV.html\"}";
//		MyHttpDelete delputMethod=new MyHttpDelete(url);
//		delputMethod.addHeader("Content-Type","application/json");
//		System.out.println("paramsStr="+paramsStr);
//		StringEntity entity=new StringEntity(paramsStr, "utf-8");
//		delputMethod.setEntity(entity);
//		return httpClient.execute(delputMethod);
//	}
}
